//
//  TSUserListTableView.m
//  chart
//
//  Created by xu on 15/8/16.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserListTableView.h"
#import "TSJournalModel.h"
#import "TSCreateScheduleViewController.h"
#import "TSReceiveMsgListModel.h"
#import "TSMessageListCell.h"
@implementation TSUserListTableView{
    TSReceiveMsgListModel *journalModel;

    NSArray *dataArray;
    NSIndexPath *mIndexPath;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style WithId:(NSString *)userId{
    if (self=[super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];

        self.dataSource = self;
        self.delegate = self;
        self.userId = userId;
        journalModel = [[TSReceiveMsgListModel alloc] init];
        [self.header beginRefreshing];
    }
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
//    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
//    }
//    
//    if (dataArray && dataArray.count > indexPath.row) {
//        TSJournalItemModel *item_model = [dataArray objectAtIndex:indexPath.row];
//        cell.textLabel.text = item_model.title;
//        [cell.textLabel setFont:FONT(12.f)];
//        cell.detailTextLabel.text = [TSTools getJournalStatusTypeString:item_model.runStatus.integerValue];
//        [cell.detailTextLabel setFont:FONT(12.f)];
//    }
//    
//    return cell;
    
    static NSString *cell_identify = @"cellIdentify";
    TSMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
    
    if (indexPath.row == 0) {
        cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify isHeader:true];
        return cell;
    }
    else if (!cell) {
        cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    if (dataArray) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:indexPath.row-1];
        cell.isSendNotReceive = 2;
        [cell setModel:model];
    }
    
    return cell;


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray) {
        return [dataArray count];
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0){return;}
    mIndexPath = indexPath;
    if (dataArray && dataArray.count > indexPath.row) {
        UIActionSheet *action_sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"删除",@"修改", @"完成", nil];
        [action_sheet showInView:[self getViewController].view];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
        [self deleteSchedule];
    }else if (buttonIndex == 1){
        [self showJournalViewController];
    }
    else if(buttonIndex==2){
        [self finishJournal];
    }
}

-(void)deleteSchedule
{
   
    TSReceiveMsgModel *model = [dataArray objectAtIndex:mIndexPath.row-1];
    WS(weakSelf);
    journalModel.requestParams = @{TSDictionaryKeyDefine_Id:model.msgId};
    [journalModel fetchWithPath:kRequestUrl_Journal_delete completionHandler:^(BOOL isSucceeded, NSError *error) {
        
        if (!weakSelf) {
            return;
        }
        [weakSelf.header beginRefreshing];
        
    } NoProgress:NO];
}
-(void)updateSchedule
{
    TSJournalItemModel *item_model = [dataArray objectAtIndex:mIndexPath.row];
    UIViewController *view_controller = [self getViewController];
    if (view_controller) {
        TSCreateScheduleViewController *schedule_vc = [[TSCreateScheduleViewController alloc] init];
        schedule_vc.journalId = item_model.msgId;
        schedule_vc.isSelf = self.userId?NO:YES;
        schedule_vc.isUpdate = true;
        [view_controller.navigationController pushViewController:schedule_vc animated:YES];
    }
}

-(void)showJournalViewController
{
    TSJournalItemModel *item_model = [dataArray objectAtIndex:mIndexPath.row-1];
    UIViewController *view_controller = [self getViewController];
    if (view_controller) {
        TSCreateScheduleViewController *schedule_vc = [[TSCreateScheduleViewController alloc] init];
        schedule_vc.journalId = item_model.msgId;
        schedule_vc.isSelf = self.userId?NO:YES;
        [view_controller.navigationController pushViewController:schedule_vc animated:YES];
    }
}

-(void)finishJournal
{
    WS(weakSelf);
    TSJournalItemModel *item_model = [dataArray objectAtIndex:mIndexPath.row-1];
    item_model.requestParams = @{TSDictionaryKeyDefine_Id:item_model.msgId};
    [item_model fetchWithPath:kRequestUrl_Journal_finish completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        
    } NoProgress:YES];

}

#pragma mark - requestData

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    
    NSString *url = kRequestUrl_Journal_outList_journal;
    journalModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    
    if (self.userId) {
        url = kRequestUrl_Journal_outList_pointList_user;
        journalModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize), TSDictionaryKeyDefine_Id:self.userId?:@""};
    }

    [journalModel fetchWithPath:url completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (journalModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->dataArray = journalModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    journalModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [journalModel fetchWithPath:kRequestUrl_Journal_outList_journal completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (journalModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            strongSelf->dataArray = journalModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

@end
